<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Top 50 rulers' );

$query = "SELECT name, score, last_year_score, ruler_id, experience, resigned FROM ruler ORDER BY score DESC LIMIT 0, 50 ";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num= mysql_numrows( $result );

if( isset( $_SESSION[ 'ruler_ranks_status' ] ) ) {
	print( $_SESSION[ 'ruler_ranks_status' ] );
	unset( $_SESSION[ 'ruler_ranks_status' ] );
}
?>
					<div class="blok">
						<?php transprint( 'Get information on a ruler' ); ?>: 
						<form method="post" action="forward_ruler.php">
							<input type="text" name="ruler_name" />
	  						<input type="submit" value="submit" />
	  					</form>
					</div>
					<br />
					<table>
						<tr><th colspan="7"><?php transprint( 'Top 50 rulers' ); ?></th></tr>
						<tr>
							<td><?php transprint( 'Rank' ); ?></td>
							<td><?php transprint( 'Name' ); ?></td>
							<td align="right"><?php transprint( 'Villages' ); ?></td>
							<td align="right"><?php transprint( 'Score' ); ?></td>
							<td align="right"><?php transprint( 'Growth' ); ?></td>
							<td align="right"><?php transprint( 'Battle score' ); ?></td>
							<td align="right"><?php transprint( 'Battle rank' ); ?></td>
						</tr>
<?php
$i = 0;
while ( $i < $num ) {
    if( $i % 2 == 0 ) {
        print "<tr id=\"even\">";
    }
    else {
        print "<tr>";
    }
	$query = "SELECT count(*) FROM ruler WHERE score > " . mysql_result( $result, $i, "score" );
	$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$rank = number_format( $row2[ 'count(*)' ] + 1 );
	$query = "SELECT count(*) FROM village WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
	$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
    $villages = $row2[ 'count(*)' ];
    if( mysql_result( $result, $i, "resigned" ) == 1 ) {
    	$resigned = " (" . translate ( "resigned" ) . ")";
    }
    else {
    	$resigned = "";
    }
	
	if( mysql_result( $result, $i, "last_year_score" ) > 0 && mysql_result( $result, $i, "score" ) > 0 ) {
		$growth = round( ( mysql_result( $result, $i, "score" ) / mysql_result( $result, $i, "last_year_score" ) - 1.0 ) * 100.0, 2 ) . "%";
	}
	else {
		$growth = '-';
	}
    
    print "<td align=\"right\">" . ( $rank ) . "</td>" .
    		"<td><a href=\"ruler.php?name=" .  mysql_result( $result, $i, "name" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>$resigned</td>" .
    				"<td align=\"right\">$villages</td>" .
    				"<td align=\"right\">" .  number_format( mysql_result( $result, $i, "score" ) ) . "</td>" .
    				"<td align=\"right\">$growth</td>" .
    				"<td align=\"right\">" .  number_format( mysql_result( $result, $i, "experience" ) );
    if( mysql_result( $result, $i, "experience" ) == 0 ) {
        print "</td><td align=\"right\">-</td></tr>\r\n";
    }
    else {
		$query = "SELECT count(*) FROM ruler WHERE experience > " . mysql_result( $result, $i, "experience" );
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
        print "</td><td align=\"right\">" . number_format( $row2[ 'count(*)' ] + 1 ) . "</td></tr>\r\n";
    }
    $i ++;
}
?>
				</table>
<?php
aos_footer();
?>